<template>
  <div>
    <div class="flex_between">
      <el-button type="primary" @click="addGive">新增赠品</el-button>
      <el-button @click="$emit('send')">返回</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="赠品名称" prop="gift_name" />
        <el-table-column label="商品名称" prop="goods_name" />
        <el-table-column label="开始日期" prop="start_at" />
        <el-table-column label="结束" prop="end_at" />
        <el-table-column label="状态" prop="status_name" />
        <el-table-column label="操作" prop="address">
          <template slot-scope="scoped">
            <el-button type="text" @click="delList(scoped.row)">编辑</el-button>
            <el-button type="text" @click="DelList(scoped.row)">失效</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :before-close="handleClose"
      title="赠品"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form ref="form" label-width="100px" :model="form" style="width: 70%">
        <el-form-item label="赠品名称">
          <el-input
            v-model="form.gift_name"
            placeholder="请输入赠品名称"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="活动商品：">
          <el-button v-if="addgose == true" type="primary" @click="addGoods">
            添加商品
          </el-button>
          <span v-if="addgose == false">{{ are }}</span>
          <span v-if="addgose == false">
            <el-button type="text" @click="addGoods">修改</el-button>
          </span>
        </el-form-item>
        <el-form-item label="有效期">
          <el-date-picker
            v-model="value1"
            end-placeholder="结束日期"
            range-separator="至"
            start-placeholder="开始日期"
            type="daterange"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="限领次数">
          <el-input
            v-model="form.limit_num"
            placeholder="请输入单人限领次数"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <!-- <el-button type="primary" @click="onSubmit">立即创建</el-button> -->
          <!-- <el-button @click="goBack">取消</el-button> -->
        </el-form-item>
      </el-form>

      <el-dialog
        append-to-body
        title="添加商品"
        :visible.sync="innerVisible"
        width=""
      >
        <el-table :data="goodsTableData" style="width: 100%">
          <el-table-column label="商品名称" prop="goods_name" />
          <el-table-column label="规格" prop="goods_attr_name" />
          <el-table-column label="库存" prop="goods_attr_stock" />
          <el-table-column label="操作" prop="address">
            <template slot-scope="scoped">
              <el-button type="text" @click="addgd(scoped.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  //
  import { getGoodsList, giftList, giftAdd, giftDel } from '@/api/public'

  export default {
    components: {},
    data() {
      return {
        value1: '',
        are: '',
        emptyText: ' ',
        tableData: [],
        goodsTableData: [],
        dialogVisible: false,
        innerVisible: false,
        addgose: true,

        form: {
          gift_name: '',
          start_at: '',
          end_at: '',
          limit_num: 1,
        },
      }
    },
    watch: {},
    mounted() {
      this.init()
    },
    created() {},
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const data = {
          goods_type: 'all',
        }
        getGoodsList(data).then((res) => {
          this.goodsTableData = []
          res.data.forEach((e) => {
            if (e.type_code !== 'curriculum') {
              this.goodsTableData.push(e)
            }
          })
        })
        giftList().then((res) => {
          this.tableData = res.data
          loading.close()
          if (res.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      DelList(e) {
        if (e.status == 'Y') {
          const data = {
            id: e.id,
            status: 'N',
          }
          giftDel(data).then(() => {
            this.init()
          })
        } else {
          const data = {
            id: e.id,
            status: 'Y',
          }
          giftDel(data).then(() => {
            this.init()
          })
        }
      },
      submit() {
        this.form.start_at = this.value1[0]
        this.form.end_at = this.value1[1]
        giftAdd(this.form).then(() => {
          this.dialogVisible = false
          this.init()
        })
      },
      addGoods() {
        this.innerVisible = true
      },
      addgd(e) {
        this.form.attribute_id = e.goods_attr_id
        this.form.goods_id = e.goods_id
        this.are = e.goods_name + '(' + e.goods_attr_name + ')'
        this.innerVisible = false
        this.addgose = false
      },
      addGive() {
        this.addgose = true
        this.dialogVisible = true
        this.form = {
          gift_name: '',
          goods_id: '',
          start_at: '',
          end_at: '',
          limit_num: '1',
        }
        this.value1 = ''
      },
      delList(e) {
        this.value1 = [e.start_at, e.end_at]
        this.form.attribute_id = e.attribute_id
        this.form.goods_id = e.goods_id
        this.form.id = e.id
        this.form.gift_name = e.gift_name
        this.form.goods_name = e.goods_name
        this.form.limit_num = e.limit_num
        this.are = e.goods_name
        this.dialogVisible = true
        this.addgose = false
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
    },
  }
</script>

<style scoped>
  .flex_between {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
